<?php

class Country_Model_DbTable_Countrymembers extends Engine_Db_Table
{
  protected $_rowClass = 'Country_Model_Countrymember';
  
  const MEMBER = 1;
  const REQUEST = 2;
  const INVITE = 3;
  const NOMEMBER = 4;
  
  public function isMember($coutry_id,$use_id){
    $row = $this->getMember($use_id, $coutry_id);
    if (!$row) return false;
    return true;
  }
  
  public function joinMember($user_id,$country_id){
      $status = $this->getStatusMember($user_id, $country_id);
      if ( $status == self::NOMEMBER ) {
         $row = $this->createRow();
         $row->user_approved = 1;
         $row->country_id = $country_id;
         $row->user_id = $user_id;
         $row->save();
         return;
      } 
      if ( $status == self::INVITE ) {
          $row = $this->getMember($user_id, $country_id);
          if ($row){
            $row->user_approved = 1;
            $row->save();
            return;
          } else {
            $row = $this->createRow();
             $row->user_approved = 1;
             $row->country_id = $country_id;
             $row->user_id = $user_id;
             $row->save();
             return;
          }
      }
  }
  
  public function leaveMember($user_id, $country_id){
     $db = $this->getAdapter();
     try {
        $managerT = Engine_Api::_()->getDbtable('managers','country');
        $managerT->removeManager($country_id, $user_id);
        
        $row = $this->getMember($user_id, $country_id);
        if ($row) $row->delete();
        
        $db->commit();
     } catch (Exception $e){
        $db->rollBack();
     }
     
     
  }
  public function inviteMember($user_id, $country_id){
    //var_dump($user_id);die;
    //print_r($user_id);die;
      $status = $this->getStatusMember($user_id, $country_id);
      if ( $status == self::NOMEMBER ) {
         $row = $this->createRow();
         $row->user_approved = 0;
         $row->country_id = $country_id;
         $row->user_id = $user_id;
         $row->save();
      } 
  }
  public function inviteMember1($user_id, $country_id){
    //var_dump($user_id);die;
    //print_r($user_id);die;
      $status = $this->getStatusMember($user_id, $country_id);
      if ( $status == self::NOMEMBER ) {
         $row = $this->createRow();
         $row->user_approved = 0;
         $row->country_id = $country_id;
         $row->user_id = $user_id;
         $row->save();
      } 
  }
  public function getStatusMember($user_id, $country_id){
     $select = $this->select()
                    ->where('user_id = ?',$user_id)
                    ->where('country_id = ?',$country_id)
                    ->limit(1);
     $row = $this->fetchRow($select);
     if (!$row) return self::NOMEMBER;
     if ($row){
         if ($row->user_approved == 1) return self::MEMBER;
         if ($row->user_approved == 0) return self::INVITE;
     } 
     return self::NOMEMBER;
  }
  
  public function getMember($user_id, $country_id){
     $select = $this->select()
                    ->where('user_id = ?',$user_id)
                    ->where('country_id = ?',$country_id)
                    ->limit(1);
     $row = $this->fetchRow($select);
     return $row;
  }
  
  public function getListMemberIds ($user_id, $country_id){
    $select = $this->select()
                    ->from($this,'user_id')
                    //->where('user_id = ?',$user_id)
                    ->where('country_id = ?',$country_id);
    $ids = array();
                   // ->limit(1);
     $rows = $this->fetchAll($select);
     $array = $rows->toArray();
     foreach($array as $row){
        $ids[] = $row['user_id'] ;
     }
     return $ids;
  }
  
  
  
         
}
